package code.c09;

/**
 * @author thinkpad - 83start
 * @version v1.0
 * @create 2022/10/31 22:32
 * @package code.e09
 * @description 线程的同步机制
 */

public class Ex03 {
    public static void main(String[] args) {
        Window window = new Window();
        Thread thread1 = new Thread(window);
        Thread thread2 = new Thread(window);
        Thread thread3 = new Thread(window);

        thread1.setName("窗口1");
        thread2.setName("窗口2");
        thread3.setName("窗口3");

        thread1.start();
        thread2.start();
        thread3.start();
    }
}


class Window implements Runnable {
    private int ticket = 100;


//    final Object obj = new Object();

    //    @Override
//    public void run() {
//        while (true) {
//            synchronized (obj) {
//                if (ticket > 0) {
//                    System.out.println(Thread.currentThread().getName() + ":买票，票号 " + ticket--);
//                }else {
//                    break;
//                }
//            }
//        }
//    }

    @Override
    public void run() {
        while (true) {

            if (ticket > 0) {
                System.out.println(Thread.currentThread().getName() + ":买票，票号 " + ticket--);
            } else {
                break;
            }
        }
    }

}